package com.hzit.proxy.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzit.net.util.WXNetUtil;
import com.hzit.proxy.base.AccessToken;

import java.util.List;

public class UserListProxy {

    public List<String> getUserList(String nextOpenid){
        String url="";
        String token = AccessToken.getToken();
        if(nextOpenid==null){
            url="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+token;
        }else {
            url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + token + "&next_openid=" + nextOpenid;
        }
        String result = WXNetUtil.get(url);

        JSONObject jsonObject = JSONObject.parseObject(result);
        if(jsonObject.containsKey("data")){
            JSONObject data = jsonObject.getJSONObject("data");
            if(data.containsKey("openid")){
                JSONArray jsonArray = data.getJSONArray("openid");
                List<String> userList = jsonArray.toJavaList(String.class);
                return userList;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        UserListProxy userListProxy = new UserListProxy();
        List<String> userList = userListProxy.getUserList(null);
        System.out.println(userList);
    }
}
